package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.InspectionRuleProjectDTO;
import com.neusoft.neuiotms.dcom.model.InspectionRuleProject;
import com.neusoft.neuiotms.dcom.model.InspectionRuleProjectConcrete;
import com.neusoft.neuiotms.dcom.model.InspectionRuleProjectItem;
import com.neusoft.neuiotms.dcom.repository.InspectionRuleProjectConcreteRepository;
import com.neusoft.neuiotms.dcom.repository.InspectionRuleProjectItemRepository;

@Component
public class InspectionRuleProjectConverter extends AbstractConverter<InspectionRuleProject, InspectionRuleProjectDTO> {

    @Autowired
    private InspectionRuleProjectItemConverter projectItemConverter;

    @Autowired
    private InspectionRuleProjectItemRepository projectItemRepository;

    @Autowired
    private InspectionRuleProjectConcreteConverter projectConcreteConverter;

    @Autowired
    private InspectionRuleProjectConcreteRepository projectConcreteReposiotry;

    @Override
    public InspectionRuleProject toModel(final InspectionRuleProjectDTO dto) {

        final InspectionRuleProject model = new InspectionRuleProject();
        model.setLocationCode(dto.getLocationCode());
        model.setLocationName(dto.getLocationName());
        model.setProjectType(dto.getProjectType());
        model.setTypeCode(dto.getTypeCode());
        model.setTypeName(dto.getTypeName());

        final List<InspectionRuleProjectItem> items = model.getInspectionItem().stream().collect(Collectors.toList());
        items.forEach(t -> {
            model.getInspectionItem().remove(t);
            this.projectItemRepository.delete(t);
        });
        if ((dto.getInspectionItem() != null) && !dto.getInspectionItem().isEmpty()) {
            dto.getInspectionItem().stream().map(t -> {
                final InspectionRuleProjectItem item = this.projectItemConverter.toModel(t);
                item.setType(model.getTypeCode());
                item.setInspectionRuleProject(model);
                return item;
            }).forEach(model.getInspectionItem()::add);
        }

        final List<InspectionRuleProjectConcrete> concretes =
                model.getProjectConcrete().stream().collect(Collectors.toList());
        concretes.forEach(t -> {
            model.getProjectConcrete().remove(t);
            this.projectConcreteReposiotry.delete(t);
        });
        if ((dto.getProjectConcrete() != null) && !dto.getProjectConcrete().isEmpty()) {
            dto.getProjectConcrete().stream().map(t -> {
                final InspectionRuleProjectConcrete c = this.projectConcreteConverter.toModel(t);
                c.setInspectionRuleProject(model);
                return c;
            }).forEach(model.getProjectConcrete()::add);
        }
        return model;
    }

    @Override
    public InspectionRuleProjectDTO toDTO(final InspectionRuleProject model, final boolean forListView) {
        final InspectionRuleProjectDTO dto = new InspectionRuleProjectDTO();
        dto.setLocationCode(model.getLocationCode());
        dto.setLocationName(model.getLocationName());
        dto.setProjectType(model.getProjectType());
        dto.setTypeCode(model.getTypeCode());
        dto.setTypeName(model.getTypeName());
        dto.setId(model.getId());
        if ((model.getInspectionItem() != null) && !model.getInspectionItem().isEmpty()) {
            dto.setInspectionItem(this.projectItemConverter.toListDTO(model.getInspectionItem()));
        }
        if ((model.getProjectConcrete() != null) && !model.getProjectConcrete().isEmpty()) {
            dto.setProjectConcrete(this.projectConcreteConverter.toListDTO(model.getProjectConcrete()));
        }
        return dto;
    }

}
